﻿using System;
using Colorful.Cache;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Diagnostics;
using Colorful.Database;
using System.Linq;
using System.Linq.Dynamic.Core;
using Colorful;
using MongoDB.Driver;
using System.Linq.Expressions;
using Colorful.Models;
using MongoDB.Bson;
using System.Text.RegularExpressions;
using System.Data;
using Colorful.CMS.Core;

namespace TestApp
{
    public enum ReportType
    {
        UVPV,
        Country,
        Area,
        Page,
        Domain
    }
    class Program
    {
        static void Compute(double current, string itemStr, List<double> list, Dictionary<string, double> result, int count = 0)
        {
            foreach (var d in list)
            {
                var sum = current + d;
                if (sum <= 215.1)
                {
                    string item;
                    if (itemStr == null)
                        item = $"{current}+{d}";
                    else
                        item = $"{itemStr}+{d}";
                    if (!result.ContainsKey(item))
                        result.Add(item, sum);
                    if (count < 3)
                    {
                        Compute(sum, item, list, result, count+1);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            var numbers = new double[] { 84.91, 75.55, 84.91, 113.65, 59.42, 113.96, 116.1, 60.71, 116.42, 113.46, 59.33, 113.78 };
            var result = new Dictionary<string, double>();
            foreach (var n in numbers)
                Compute(n, null, numbers.ToList(), result);
            var orderResult = result.OrderByDescending(a => a.Value).ToList();
            var finalResult = orderResult.First();
            Console.WriteLine("Starting...");
            //Test1();


            #region 数据库测试
            DbSetting.ConnectionString = "mongodb://slid:liguo1987!(*@localhost:9857/ColorCMS?maxPoolSize=500&minPoolSize=10";
            //DbSetting.ConnectionString = "mongodb://slid:liguo1987!(*@aliyun.7csoft.com:9857/ColorCMS?maxPoolSize=500&minPoolSize=10";
            DbSetting.Database = "ColorCMS";
            #endregion

            while (true)
            {
                var cmd = Console.ReadLine();
                if (cmd == "exit")
                    break;
                var watch = new Stopwatch();
                watch.Start();
                StartTask();
                Console.WriteLine("用时:{0}", watch.ElapsedMilliseconds);
                watch.Stop();
                //var clients = redis.Publish("ua", cmd);
                //Console.WriteLine("已发送给 {0} 个用户！", clients);
            }
        }
        static void Test1()
        {
            var url = "http://wx.qlogo.cn/mmopen/KydxAIB52xmc3CgNVX0eAicxWdhP9Oxtrsh2iahUfyianDsb78W037NTNxUlb0AHl5R4wgpbx6jTN6ibMztqwlbVhXic6tzKJTZQw/0";
            var savePath = System.Environment.CurrentDirectory + "\\test.jpg";
            HttpHelper.DownloadFile(url, savePath);
        }

        static DateTime GetLastUpdate(DbContext db, string id)
        {
            DateTime lastUpdate;
            var rs = db.ReportSchedules.Where(a => a.Id == id).FirstOrDefault();
            if (rs == null)
                lastUpdate = new DateTime(2000, 1, 1);
            else
                lastUpdate = rs.LastUpdate;
            return lastUpdate;
        }
        static void WriteLog(Exception ex)
        {

        }

        static void StartTask()
        {
            var tasks = new List<Task>();
            var redis = GetRedis();
            {
                for (var i = 0; i < 10; i++)
                {
                    redis.RightPush("ua_list", i);

                    //var task = Task.Factory.StartNew(async state =>
                    //{
                    //    //redis.Set("abc", state, TimeSpan.FromSeconds(3));
                    //    //await redis.SetAsync("abc", state, TimeSpan.FromSeconds(3));
                    //    //await redis.PublishAsync("ua", state.ToString());
                    //}, i);
                    //tasks.Add(task);
                }
            }
            Task.WaitAll(tasks.ToArray());
        }

        static RedisCache GetRedis()
        {
            return new RedisCache("", "localhost:6379,password=liguo1987,syncTimeout=5000");
        }
    }
}
